﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Core.Service;
using PersonalHealth.Data.Model;
using PersonalHealth.Data.Data;
using PersonalHealth.Data.IService;
using System.Data.Entity;
using Core.UnitOfWork;
using PersonalHealth.Data.Repository;
using System.Data;
using System.Data.SqlClient;
using System.Data.Entity.Infrastructure;
using System.Data.Common;
using System.Configuration;
using System.Data.EntityClient;
using Core.Repository;

namespace PersonalHealth.Data.Service
{
    public class MeasureUserService : Service<MeasureUser, HealthContext>, IMeasureUserService
    {
        

        public DataSet GetLastAndInvalid()
        {
            using (DbContext context = new HealthContext())
            {
                IUnitOfWork unitOfWork = new UnitOfWork(context);
                IDataRepository rep = new DataRepository(unitOfWork);

                string sql = "exec GetLastMeasureData;exec GetInvalidMeasureData";
                return rep.GetDataSet(sql);
            }
        }

        public DataTable GetLastMeasureUserList()
        {
            using (DbContext context = new HealthContext())
            {
                IUnitOfWork unitOfWork = new UnitOfWork(context);
                IDataRepository rep = new DataRepository(unitOfWork);

                string sql = "exec GetLastMeasureData";
                return rep.GetDataTable(sql);
            }
        }

        public DataTable GetInvalidMeasuredUserList()
        {
            using (DbContext context = new HealthContext())
            {
                IUnitOfWork unitOfWork = new UnitOfWork(context);
                IDataRepository rep = new DataRepository(unitOfWork);

                string sql = "exec GetInvalidMeasureData";
                return rep.GetDataTable(sql);
            }
        }

        /// <summary>
        /// 取出最近5个月的测试记录
        /// </summary>
        /// <returns></returns>
        public DataSet GetMeasuredUserList()
        {
            using (DbContext context = new HealthContext())
            {
                IUnitOfWork unitOfWork = new UnitOfWork(context);
                IDataRepository rep = new DataRepository(unitOfWork);
                DateTime currentFirst = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);

                string sql = string.Empty;
                for (int i = 0; i < 5; i++)
                {
                    DateTime monthFirst = currentFirst.AddMonths(-4 + i);
                    DateTime monthLast = new DateTime(monthFirst.Year, monthFirst.Month, DateTime.DaysInMonth(monthFirst.Year, monthFirst.Month));
                    sql += string.Format("exec GetMeasureData '{0}' ,'{1}' \r\n;", monthFirst, monthLast);
                }
                return rep.GetDataSet(sql);
            }
            
        }

        public MeasureUser GetMeasureUserDetail(int id)
        {
            using (DbContext context = new HealthContext())
            {
                 IUnitOfWork unitOfWork = new UnitOfWork(context);
                 IMeasureUserRepository rep = new MeasureUserRepository(unitOfWork);
                 return rep.GetMeasureUserDetail(id);
            }
        }

        public IList<MeasureUser> GetMeasureUserDetailList<Tkey>(Func<MeasureUser, bool> expression,
         Func<MeasureUser, Tkey>[] orderby, bool[] isDescending, int pageIndex, int pageCount, out int pages, out int records)
        {
            using (DbContext context = new HealthContext())
            {
                IUnitOfWork unitOfWork = new UnitOfWork(context);
                unitOfWork.LazyLoadingEnabled = false;
                unitOfWork.ProxyCreationEnabled = false;

                IMeasureUserRepository rep = new MeasureUserRepository(unitOfWork);

                return rep.GetMeasureUserDetailList(expression, orderby, isDescending, pageIndex, pageCount, out pages, out records);
            }
        }

        public DataTable GetMsgTask(DateTime start,DateTime end,int userId,int pageIndex,int pageSize,out int totalRecords)
        {
            using (DbContext context = new HealthContext())
            {
                IUnitOfWork unitOfWork = new UnitOfWork(context);
                IDataRepository rep = new DataRepository(unitOfWork);
                int total = 0;
                string sql = string.Format("exec GetMsgTask '{0}','{1}',{2},{3},{4},{5}", start, end, userId, pageIndex, pageSize,total);

                DataSet ds = rep.GetDataSet(sql);
                totalRecords = int.Parse(ds.Tables[1].Rows[0]["totalRecords"].ToString());
                return ds.Tables[0];
            }
        }
    }
}
